package com.mysoft.gfyx.clue.controller.option;

import com.mysoft.framework.modeling.OptionDataController;
import com.mysoft.framework.modeling.dto.Filter;
import com.mysoft.framework.modeling.dto.OptionItem;
import com.mysoft.framework.rpc.annotation.PubService;
import com.mysoft.framework.rpc.contants.RequestPrefix;
import com.mysoft.gfyx.clue.remoteservice.RemoteAreaService;
import com.mysoft.gfyx.clue.remoteservice.RemoteProfitService;
import com.mysoft.gfyx.clue.remoteservice.dto.AreaDto;
import com.mysoft.gfyx.clue.remoteservice.dto.GetProfitProvinceDataDto;
import com.mysoft.gfyx.clue.remoteservice.dto.ProvinceAndCityConditionDto;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Tag(name = "地区备选项-API")
@PubService(value = "/areaOptionData", prefix = RequestPrefix.API, method = {})
public class AreaOptionDataController extends OptionDataController {

    @Autowired
    private RemoteAreaService remoteAreaService;

    @Autowired
    private RemoteProfitService remoteProfitService;

    @Override
    protected List<OptionItem> load(List<Filter> filters, Map<String, String> urlParams) {
        List<OptionItem> items = new ArrayList<>();
        GetProfitProvinceDataDto getProfitProvinceDataDto = remoteProfitService.getProfitProvince();
        ProvinceAndCityConditionDto conditionDto = new ProvinceAndCityConditionDto();
        List<AreaDto> res;
        if (getProfitProvinceDataDto.getProvinceNumType().equals(RemoteProfitService.ProvinceNumType.Province.toString())
                && getProfitProvinceDataDto.getProvinceList().size() > 0) {//如果是省份权益，需要过滤省份
            List<String> provinceCode = new ArrayList<>();
            getProfitProvinceDataDto.getProvinceList().forEach(m -> {
                provinceCode.add(m.getValue());
            });
            conditionDto.setProvinceCode(provinceCode);
            res = remoteAreaService.getProvinceAndCity(conditionDto);
        } else {
            conditionDto.setIsIncludeNationwide(true);

            OptionItem item = new OptionItem();
            item.setValue("0");
            item.setParent("-1");
            item.setText("全国");
            items.add(item);
            res = remoteAreaService.getAll();
        }

        res.forEach(areaDto -> {
            OptionItem item = new OptionItem();
            item.setValue(areaDto.getItemCode());
            item.setParent(areaDto.getParentCode());
            item.setText(areaDto.getItemName());
            items.add(item);
        });

        return items;
    }
}
